package mammuthus.template.zookeeper

import mammuthus.protocal.WriteFileTaskResponse
import mammuthus.master.Master
import java.util
import mammuthus.tool.TemplateTool
import mammuthus.template.TemplateProcessor

/**
 * 5/3/15 WilliamZhu(allwefantasy@gmail.com)
 */
class MyIdProcessor(master: Master) extends TemplateProcessor {
  override def process(fileName: String, targetFilePath: String, params: Map[String, AnyRef]): List[WriteFileTaskResponse] = {
    val hostNames = params("slaves").asInstanceOf[String]
    val installPath = params("installPath").asInstanceOf[String]
    val appName = params("appName").asInstanceOf[String]
    val app = params.getOrElse("app",appName)
    val idAndHosts = new util.ArrayList[IdAndHost]()
    hostNames.split(",").sorted.toList.zipWithIndex.flatMap {
      case (host: String, pos: Int) =>
        idAndHosts.add(new IdAndHost(host, pos))
        val configStr = TemplateTool.render(s"/template/app/$app/my.id.vm", Map(
          "myId" -> pos.toString
        ))
        master.writeFile(hostNames, configStr,targetFilePath,false)
    }.toList

  }
}
